<?php 

class EmailVerification {
	var $Context;
	var $Name;
	var $User;
	var $VerificationKey;
	var $s;
	
	function CreateKey() {
		//create key
		$this->VerificationKey = DefineVerificationKey();
		
		//save it in the database
		$this->s->Clear();
		$this->s->SetMainTable('User', 'u');
		$this->s->AddFieldNameValue('EmailVerificationKey', $this->VerificationKey, 1);
		$this->s->AddWhere('u', 'UserID', '', $this->User->UserID, '=');
		$this->Context->Database->Update($this->s, $this->Name, 'CreateKey', 'An error occurred while managing your account information.');
	}
	
	//Constructor
	function EmailVerification(&$Context, $User) {
		$this->Context = &$Context;
		$this->Name = 'EmailVerification';
		$this->User = $User;
		$this->s = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
	}
	
	
	function MailKey($subject, $File, $DefaultPath, $URL, $PostBackAction) {
		if ( !$EmailBody = $this->GetTemplateBody($File, $DefaultPath) ) $this->Context->WarningCollector->Add('Failed to read email template ('.$File.')');
		else {
			$e = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Email');
			$e->HtmlOn = 0;
			$e->WarningCollector = &$this->Context->WarningCollector;
			$e->ErrorManager = &$this->Context->ErrorManager;
			$e->AddFrom($this->Context->Configuration['SUPPORT_EMAIL'], $this->Context->Configuration['SUPPORT_NAME']);
			$e->AddRecipient($this->User->Email, $this->User->Name);
			$e->Subject = $subject;
			$e->Body = str_replace(
				array("{user_name}", "{forum_name}", "{validation_url}", "{forum_email}", "{forum_url}", "{forum_support_name}"),
				array(
					$this->User->Name,
					$this->Context->Configuration['APPLICATION_TITLE'],
					ConcatenatePath(
						$this->Context->Configuration['BASE_URL'],
						GetUrl($this->Context->Configuration, $URL, '', '', '', '', 'PostBackAction='.$PostBackAction.'&u='.$this->User->UserID.'&k='.$this->VerificationKey)
					),
					$this->Context->Configuration['SUPPORT_EMAIL'],
					$this->Context->Configuration['BASE_URL'], 
					$this->Context->Configuration['SUPPORT_NAME']
				),
				$EmailBody
			);
			$e->Send();
		}
	}
	
	function GetTemplateBody($File, $Path) {
		$TemplateBody = array();
		$TemplateBody[] = $this->Context->Configuration['APPLICATION_PATH']
						.'conf/'.$File;
		$TemplateBody[] = $this->Context->Configuration['LANGUAGES_PATH']
						.$this->Context->Configuration['LANGUAGE']
						.'/'.$File;
		$TemplateBody[] = $Path.'/'.$File;
		
		for ($i = 0; $i < count($TemplateBody); $i++) {
			$EmailBody = @file_get_contents($TemplateBody[$i]);
			if ($EmailBody) return $EmailBody;
		}
		
		return false;
	}
	
	function GetKey() {
		$this->s->Clear();
		$this->s->SetMainTable('User', 'u');
		$this->s->AddSelect('EmailVerificationKey', 'u');
		$this->s->AddWhere('u', 'UserID', '', $this->User->UserID, '=');
		$UserResult = $this->Context->Database->Select($this->s, $this->Name, 'GetKey', 'An error occurred while retrieving account information.');
		while ($rows = $this->Context->Database->GetRow($UserResult)) {
			if ($this->VerificationKey = ForceString($rows['EmailVerificationKey'], '')) return true;
			else return false;
		}
		
	}
	
	function DeleteKey() {
		$this->s->Clear();
		$this->s->SetMainTable('User', 'u');
		$this->s->AddFieldNameValue('EmailVerificationKey', '', 1);
		$this->s->AddWhere('u', 'UserID', '', $this->User->UserID, '=');
		$this->Context->Database->Update($this->s, $this->Name, 'DeleteKey', 'An error occurred while managing your account information.');
	}
}

?>